package org.siam.impl.schemas {
	
	import flash.utils.Dictionary;
	
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.properties.ISchemaProperty;
	import org.siam.impl.schemas.properties.SchemaProperty;

	public class Schema implements ISchema {
		
		public static const NULL:ISchema = new SchemaNull();
		
		private var _propertyIds:Array;
		private var _properties:Dictionary;
		internal var _id:String;
		private var _metadata:Dictionary;
		
		public function Schema() {
			_propertyIds = new Array();
			_properties = new Dictionary();
			_metadata = new Dictionary();
		}
		
		public function get id():String {
			return _id;
		}
		
		public function hasProperty(id:String):Boolean {
			return _properties.hasOwnProperty(id);
		}
		
		public function findProperty(id:String):ISchemaProperty {
			return hasProperty(id) ? _properties[id] : SchemaProperty.NULL;
		}
		
		public function get propertyIds():Array {
			return _propertyIds.slice(); // _propertyIds must be cloned!
		}
		
		public function findMetadata(metaKey:String):String { 
			return hasMetadata(metaKey) ? _metadata[metaKey] : null;
		}
		
		internal function registerMetadata(metaKey:String, metaValue:String):void { 
			_metadata[metaKey] = metaValue;
		}
		
		private function hasMetadata(metaKey:String):Boolean {
			return _metadata.hasOwnProperty(metaKey);
		}
		
		internal function addProperty(property:ISchemaProperty):void {
			var propertyId:String = property.id;
			if (!hasProperty(propertyId)) {
				_properties[propertyId] = property;
				_propertyIds.push(propertyId);
			}
		}
	}
}